package com.example.recall_application.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmsUtil {
    public static Uri SMS_INBOX = Uri.parse("content://sms/");
    public static List<String> smsnumbers_display = new ArrayList<>();
    public static List<String> smscontent_display = new ArrayList<>();
    public static List<Integer> smstype_display = new ArrayList<>();
    public static List<Long> smstype_date = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static List<String> getSmsFromPhone(Context context,String lastTime,int type_num) {
        if (context.checkSelfPermission(android.Manifest.permission.READ_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
        List<String> smslist=new ArrayList<> ();
        smstype_date.clear();
        smstype_display.clear();
        smscontent_display.clear();
        smsnumbers_display.clear();
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[] {"_id", "address", "person","body", "date", "type" };
        Cursor cur;
        if(type_num == 1){
            cur = cr.query(SMS_INBOX, projection, "date > ? and type = 2", new String[]{lastTime}, "date desc");
        }else{
            cur = cr.query(SMS_INBOX, projection, "date > ?", new String[]{lastTime}, "date desc");
        }

        if (null == cur) {
            Log.i("ooc","************cur == null");
            return null;
        }
        Log.d("testsms cur",cur.toString());
        while(cur.moveToNext()) {
            Long date = cur.getLong(cur.getColumnIndex("date"));
            int type = cur.getInt(cur.getColumnIndex("type"));
            //if(type==2 && System.currentTimeMillis()-date>Integer.parseInt(MainActivity.P2)*24*60*60*1000){
            //    continue;
            //}
            //if(type==1 && System.currentTimeMillis()-date>Integer.parseInt(MainActivity.P3)*60*60*1000){
            //    continue;
            //}
            String number = cur.getString(cur.getColumnIndex("address"));//手机号
            String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            String body = cur.getString(cur.getColumnIndex("body"));//短信内容
            //至此就获得了短信的相关的内容, 以下是把短信加入map中，构建listview,非必要。
            Log.d("testsms",""+number);
            smsnumbers_display.add(number);
            smscontent_display.add(body);
            smstype_display.add(type);
            smstype_date.add(date);
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("num",number);
            map.put("mess",body);
            if(number==null)
                break;
            if(smslist.size()==0){
                smslist.add(number);
                continue;
            }
            for(int i=0;i<smslist.size();i++) {
                if (smslist.get(i)!=null&&smslist.get(i).equals(number)){
                    Log.d("getsmslist","equal:"+number);
                    break;
                }
                if(i==smslist.size()-1)
                    smslist.add(number);
            }
        }
        if(type_num == 1){
            return smslist;
        }
        else {
            return smscontent_display;
        }

    }
}

